Take Home Exercise 5

Visualising and Analysing Geographic and Movement Data

JUNQIU NI (MITB)
2022-05-29

1. Overview

In this take home exercise, I will reveal Geographic and Movement Data of the city of Engagement, Ohio USA.

2. Getting Started

2.1 Loading R packages

Before we get started, it is important to ensure that the R packages have been installed. If yes, we will load the R packages. If they have yet to be installed, we will install the R packages and load them onto R environment.

The chunk code below will do the trick.

packages = c('tidyverse','sf','tmap','lubridate','clock','sftime','rmarkdown')

for(p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

2.2. Importing Data

The code chunk below import “Schools.csv” “Buildings.csv” “Apartments.csv” “Pubs.csv” “employers.csv” and “Restaurants.csv” from the data folder into R by using [read_sf] from [sf], an R package specially designed to handle geospatial data in simple feature objects.

schools <- read_sf("/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/in_class_ex/data/wkt/Schools.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
buildings<- read_sf("/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/in_class_ex/data/wkt/Buildings.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
apartments <- read_sf("/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/in_class_ex/data/wkt/Apartments.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
pubs <- read_sf("/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/in_class_ex/data/wkt/Pubs.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
employers <- read_sf("/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/in_class_ex/data/wkt/employers.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")
restaurants <- read_sf("/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/in_class_ex/data/wkt/Restaurants.csv", 
                   options = "GEOM_POSSIBLE_NAMES=location")

3. Plotting the map

3.1 Plotting the map with tmap

The code chunk below plots the building polygon features by using [tm_polygon()].

tmap_mode("plot")
tm_shape(buildings)+
tm_polygons(col = "grey60",
           size = 1,
           border.col = "black",
           border.lwd = 1) +
tm_shape(employers) +
  tm_dots(col = "red") +
tm_shape(schools) +
  tm_dots(col = "yellow") +
tm_shape(pubs) +
  tm_dots(col = "blue") +
tm_shape(restaurants) +
  tm_dots(col = "green") +
tm_shape(apartments) +
  tm_dots(col = "lightblue")

3.2 Interactive map with tmap

In the code below, tmap_mode(“plot”) is changed to tmap_mode(“view”) to interactively show the plot.

tmap_mode("view")
tm_shape(buildings)+
tm_polygons(col = "grey60",
           size = 1,
           border.col = "black",
           border.lwd = 1) +
tm_shape(employers) +
  tm_dots(col = "red") +
tm_shape(schools) +
  tm_dots(col = "yellow") +
tm_shape(pubs) +
  tm_dots(col = "blue") +
tm_shape(restaurants) +
  tm_dots(col = "green") +
tm_shape(apartments) +
  tm_dots(col = "lightblue")

3.3 Qualitative thematic map by building type

The building footprints are coloured by buildingType field.

tmap_mode("plot")
tm_shape(buildings)+
tm_polygons(col = "buildingType",
           palette="Accent",
           border.col = "black",
           border.alpha = .5,
           border.lwd = 0.5)
tmap_mode("plot")

3.4 Interactive map with tmap

In the code below, tmap_mode(“plot”) is changed to tmap_mode(“view”) to interactively show the plot.

tmap_mode("view")
tm_shape(buildings)+
tm_polygons(col = "buildingType",
           palette="Accent",
           border.col = "black",
           border.alpha = .5,
           border.lwd = 0.5)
tmap_mode("view")

3.5 Conclusion

The map above shows the commercial, residential and other areas of the city. The commercial area have relatively more employers while residential area consists of many apartments.

4. Movement Data

4.1 Import the big data and save the subset data to rds format

We will choose the 6 days’ location data from “ParticipantStatusLogs1.csv” to represent the traffic data.

logs <- read_sf("/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/takehome_ex/data/ParticipantStatusLogs1.csv", 
                options = "GEOM_POSSIBLE_NAMES=currentLocation")
glimpse(logs)

Data Preprocessing

logs_selected <- logs %>%
  mutate(Timestamp = date_time_parse(timestamp,
                                      zone="",
                                      format="%Y-%m-%dT%H:%M:%S"))%>%
  mutate(day=get_day(Timestamp)) %>%
  filter(currentMode == 'Transport')

Save the new data needed for further use

write_rds(logs_selected,"/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/takehome_ex/data/rds/logs_selected.rds")

#4.2 Importing the data

logs_selected <- read_rds("/Users/junqiuni/Downloads/visual/pilipalabong/ISSS608/takehome_ex/data/rds/logs_selected.rds")

#4.3 Plotting the traffic data as points

tmap_mode("plot")
tm_shape(buildings)+
tm_polygons(col = "grey60",
           size = 1,
           border.col = "black",
           border.lwd = 1) +
tm_shape(logs_selected) +
  tm_dots(col = "red")

#4.4 Plotting Hexagon Binning Map to see the traffic

We use st_make_grid() of sf package is used to create haxegons.

hex <- st_make_grid(buildings, 
                    cellsize=100, 
                    square=FALSE) %>%
  st_sf() %>%
  rowid_to_column('hex_id')
plot(hex)

points_in_hex <- st_join(logs_selected, 
                         hex, 
                         join=st_within)
#plot(points_in_hex, pch='.')

st_join() of sf package is used to count the number of event points in the hexagons.

points_in_hex <- st_join(logs_selected, 
                        hex, 
                        join=st_within) %>%
  st_set_geometry(NULL) %>%
  count(name='pointCount', hex_id)
head(points_in_hex)
# A tibble: 6 × 2
  hex_id pointCount
   <int>      <int>
1    169         35
2    212         56
3    225         21
4    226         94
5    227         22
6    228         45

left_join() of dplyr package is used to perform a left-join by using hex as the target table and points_in_hex as the join table. The join ID is hex_id.

hex_combined <- hex %>%
  left_join(points_in_hex, 
            by = 'hex_id') %>%
  replace(is.na(.), 0)

tmap package is used to create the hexagon binning map.

tm_shape(hex_combined %>%
           filter(pointCount > 0))+
  tm_fill("pointCount",
          n = 8,
          style = "quantile") +
  tm_borders(alpha = 0.1)

4.5 Conclusion

The hexagon binning map suggest the area with more traffic over the period. The area with darker color means more traffic. The result is consistent with the map generated from part 3. The main traffic is among the commercial areas.